Skip to content

Fix unbounded memory growth when the app runs for hours - #122

Closed
travisketchum wants to merge 3 commits into
keefo:mainfrom
travisketchum:fix/memory-growth
Closed

travisketchum wants to merge 3 commits into
keefo:mainfrom
travisketchum:fix/memory-growth

Conversation

@travisketchum

Copy link
Copy Markdown

Problem

Left running for several hours, NeewerLite's memory grows until macOS runs out of memory. On a 64 GB M1 Max, other apps start failing with out-of-memory errors.

Cause

The main cause is Bluetooth scanning that never stops:

centralManagerDidUpdateState starts an unfiltered scan at power-on, and nothing ever stops it.
scanAction always ends by restarting the scan with CBCentralManagerScanOptionAllowDuplicatesKey: true, and the Scan view's Stop button doesn't call stopScan().

Together, these deliver every advertisement from every nearby BLE device for as long as the app is open. CoreBluetooth keeps a CBPeripheral for every device it sees, and phones, watches and AirTags rotate their addresses, so that set never stops growing. peripheralInvalidCache was never pruned either.

Changes

Time-boxed scanning (AppDelegate.swift): startScanning(duration:) and stopScanning() replace the open-ended scans, and AllowDuplicates is gone. A scan stops after 60 s, or as soon as every saved light is connected. A Scan-for-new-lights scan still runs until the user presses Stop. Known lights reconnect through connect(), which doesn't need a scan. The existing 10 s keep-alive timer starts a 60 s scan, at most once every 5 minutes, only while a saved light has never been found.

Rescan for Lights: a new item in the menu-bar menu, just above Quit, that runs a one-off scan for saved lights. It is localized in de, es, fr, ja, ko and zh-Hans.

DeviceViewObject retain cycle: the Observable.bind closures now capture [weak self]. Before, each view object and its light were never freed. This also removes the no-op 10 s timer.

AudioSpectrogram: deinit now frees the manually allocated filterBank and sgemmResult buffers, which leaked every time Listen was turned off. startVolumeListener() now removes the previous CoreAudio listener first, so they don't stack up.
Legacy MCP /sse sessions (Server.swift): sessions are now removed when the client disconnects. A 30 s keepalive comment surfaces dead connections, a write error ends the session, and its MCP context is released. Stale session IDs are dropped when a context is re-bound.

Testing

Builds with Xcode 27.1, and all 237 unit tests pass.

Ran the patched build for about 23.5 hours on macOS (M1 Max). Memory peaked at 106 MB and sat at 86 MB at the end, with no steady growth. Before this change, the same setup exhausted 64 GB within hours.

Lights respond immediately to on/off throughout the day, with no manual rescan needed.

- Time-box BLE scans and drop AllowDuplicates; saved lights reconnect
  without scanning, and a short scan runs only while one is missing
- Break DeviceViewObject retain cycle (weak self in bind closures,
  remove the no-op 10s timer)
- Free AudioSpectrogram's manually allocated buffers and stop stacking
  volume listeners
- Clean up legacy MCP SSE sessions when the client disconnects

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013kitTs6K338t9YLbyktXqU
Background scans are now time-boxed, so give the user a manual way to
start a fresh scan if a saved light doesn't reconnect.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013kitTs6K338t9YLbyktXqU
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013kitTs6K338t9YLbyktXqU
@travisketchum

Copy link
Copy Markdown
Author

Closing this. The out-of-memory crashes I saw turned out to come from another app, so I’d rather not push these changes based on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants